#!/bin/sh
# PCP QA Test No. 551
# Exercise fix for hanging-pmcd-by-drip-feeding-requests
#
# Copyright (c) 2013 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# secure/* scripts all use python, check its installed
# otherwise this test fails (./secure/* not available)
. ./common.python

host=localhost
ppid=0
count=5
status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_cleanup()
{
    cd $here
    rm -rf $tmp.*
    [ $ppid -eq 0 ] || (( kill $ppid >/dev/null 2>&1 ) &)
    ppid=0
}

cpu_count()
{
    pmprobe -h $host -v hinv.ncpu | tee -a $seq_full | awk '{ print $3 }'
}

# make sure the shell babble after $PCP_PYTHON_PROG is terminated goes
# elsewhere
#
exec 2>$tmp.err

# real QA test starts here
cd secure
ncpu=`cpu_count`
echo "Initial CPU count: $ncpu" > $seq_full
for hang_test in hang-*
do
    echo "$hang_test checking: " | tee -a $seq_full
    $PCP_PYTHON_PROG $hang_test $host >$tmp.out 2>&1 &
    ppid=$!
    sts=0
    n=0
    while [ $n -lt $count ]
    do
        ncpunow=`cpu_count`
        echo "Fetched CPU count: $ncpunow" >> $seq_full
        if [ $ncpunow != $ncpu ]
        then
            sts=1
            break
        fi
        echo "[$n] check data matched" | tee -a $seq_full
        n=`expr $n + 1`
        sleep 1
    done
    ( kill $ppid >/dev/null 2>&1 ) &
    wait $ppid
    ppid=0
    echo "$hang_test complete" | tee -a $seq_full
    echo "$hang_test output " >> $seq_full
    cat $tmp.out >> $seq_full
    [ $sts -eq 0 ] || exit
done

echo >>$seq_full
echo "==== shell's stderr ===========" >>$seq_full
cat $tmp.err >>$seq_full

# success, all done
status=0
exit
